var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/DeptTreeSelectCustom/index.tsx
var DeptTreeSelectCustom_exports = {};
__export(DeptTreeSelectCustom_exports, {
  default: () => DeptTreeSelectCustom
});
module.exports = __toCommonJS(DeptTreeSelectCustom_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@szzj/utils");
var import_hooks = __toESM(require("./hooks"));
var import_style = require("antd/es/tree-select/style/index.js");
var import_index = require("./index.less");
function DeptTreeSelectCustom({
  authed,
  initDeptDeep,
  options,
  value,
  onChange,
  popupClassName,
  searchNameService,
  findChildService,
  treeService,
  authedTreeService,
  nodeDetailService,
  ...rest
}) {
  const [innerValue, setInnerValue] = (0, import_react.useState)(void 0);
  const [expandedKeys, setExpandedKeys] = (0, import_react.useState)([]);
  const {
    fetchInitialTree,
    treeNodes,
    findNode,
    findAncestorNodes,
    loading,
    detailLoading,
    loadData,
    fetchDeptNodeDetail,
    treeNodesBySearch,
    searchTreeNodesByName,
    searchExpandedKeys,
    clearTreeNodesBySearch
  } = (0, import_hooks.default)({
    authed,
    initDeptDeep,
    searchNameService,
    findChildService,
    treeService,
    authedTreeService,
    nodeDetailService
  });
  (0, import_react.useEffect)(() => {
    if (!(options == null ? void 0 : options.length) && !treeNodes.length)
      fetchInitialTree();
  }, [options]);
  (0, import_react.useEffect)(() => {
    if (value && value !== innerValue) {
      if (!findNode(value)) {
        fetchDeptNodeDetail(value);
        setInnerValue(value);
      } else {
        setInnerValue(value);
      }
    }
  }, [value, fetchDeptNodeDetail]);
  (0, import_react.useEffect)(() => {
    if (treeNodes.length) {
      setExpandedKeys(treeNodes.map((node) => node.key));
    }
  }, [treeNodes.length]);
  (0, import_react.useEffect)(() => {
    if (innerValue) {
      const ancestorNodes = findAncestorNodes(innerValue);
      const keysToFill = ancestorNodes.filter(
        (node) => !node.isLeaf && !expandedKeys.some((key) => node.key === key)
      ).map((node) => node.key);
      setExpandedKeys([...expandedKeys, ...keysToFill]);
    }
  }, [innerValue, findAncestorNodes]);
  const handleChange = (0, import_react.useCallback)(
    (val, labelList, extra) => {
      setInnerValue(val);
      onChange == null ? void 0 : onChange(val, labelList, extra);
      clearTreeNodesBySearch();
      if (val && !findNode(val)) {
        fetchDeptNodeDetail(val);
      }
    },
    [
      onChange,
      clearTreeNodesBySearch,
      findNode,
      fetchDeptNodeDetail,
      expandedKeys,
      findAncestorNodes,
      setExpandedKeys
    ]
  );
  const handleSearch = (0, import_react.useCallback)(
    (searchValue) => {
      if (searchValue && searchValue.length >= 2) {
        searchTreeNodesByName(searchValue);
      } else if (!searchValue) {
        clearTreeNodesBySearch();
      }
    },
    [searchTreeNodesByName]
  );
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.TreeSelect,
    {
      placeholder: "请选择部门",
      showCheckedStrategy: import_antd.TreeSelect.SHOW_ALL,
      ...rest,
      popupClassName: popupClassName ? `drs-dept-tree-select-popup ${popupClassName}` : "drs-dept-tree-select-popup",
      loading: loading || detailLoading,
      loadData,
      treeData: treeNodesBySearch.length ? treeNodesBySearch : options ?? treeNodes,
      treeDefaultExpandedKeys: treeNodesBySearch.length ? treeNodesBySearch.map((node) => node.key) : (options ?? treeNodes).map((node) => node.key),
      treeExpandedKeys: treeNodesBySearch.length ? searchExpandedKeys : expandedKeys,
      onTreeExpand: treeNodesBySearch.length ? void 0 : (keys) => setExpandedKeys(keys),
      fieldNames: {
        value: "key",
        label: "title",
        children: "children"
      },
      showSearch: true,
      filterTreeNode: () => true,
      onSearch: (0, import_utils.debounce)(handleSearch, 1e3),
      onBlur: () => clearTreeNodesBySearch(),
      value: innerValue,
      onChange: handleChange
    }
  );
}
